package com.shd.edanpei.presenter;

import android.support.v4.content.ContextCompat;
import android.text.TextUtils;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.presenter.contract.NewDemandContract;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.util.ToastUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/10/18.
 * yang-wen-wen
 */

public class NewDemandPresenter extends BasePresenterImpl<NewDemandContract.NewDemandIView>
        implements NewDemandContract.NewDemandIPresenter {

    private String mIndustry;
    private String mNeedProduct;
    private String mDescribe;
    private boolean canSubmit = false;


    public NewDemandPresenter(NewDemandContract.NewDemandIView mView) {
        super(mView);
    }

    @Override
    public void onIndustry() {
        DialogUtil.listDialog(mBaseIView.getContext(), "行业选择", R.array.industry2, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mIndustry = data;
                mBaseIView.setIndustry(mIndustry);

                checkData();

            }
        });
    }

    @Override
    public void onNeedProduct() {
        DialogUtil.listDialog(mBaseIView.getContext(), "需求产品", R.array.need_product, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mNeedProduct = data;
                mBaseIView.setNeedProduct(mNeedProduct);
                if (data.equals("业务")) {
                    mBaseIView.setDesc("例：需要住房订单");
                } else if (data.equals("功能")){
                    mBaseIView.setDesc("例：增加匹配区域");
                } else if (data.equals("活动")){
                    mBaseIView.setDesc("例：增加邀请好友奖励");
                } else if (data.equals("其他")){
                    mBaseIView.setDesc("请输入...");
                }
                checkData();
            }
        });
    }

    @Override
    public void setDescribe(String str) {
        mDescribe = str;
        checkData();
    }

    @Override
    public void submit() {
        if (canSubmit) {
            mBaseIView.showLoading("提交中...");
            ApiCreator.getApi().createNeed(mIndustry, mNeedProduct, mDescribe, DaoManager.getInstance().getUserBean().getId())
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            RxBus.get().send(RxBusCode.REFRESH_NEED_LIST);
                            ToastUtil.showToast("新增成功");
                            mBaseIView.getContext().finish();
                        }

                        @Override
                        public void onFinish() {
                            mBaseIView.stopLoading();
                        }
                    });
        }
    }

    private void checkData() {
        if (TextUtils.isEmpty(mIndustry)
                || TextUtils.isEmpty(mNeedProduct)
                || TextUtils.isEmpty(mDescribe)) {
            canSubmit = false;
            mBaseIView.getSubmitView().setBackground(ContextCompat.getDrawable(mBaseIView.getContext(), R.drawable.shape_btn_unclick));
            return;
        }
        canSubmit = true;
        mBaseIView.getSubmitView().setBackground(ContextCompat.getDrawable(mBaseIView.getContext(), R.drawable.shape_main_button));
    }
}
